//返回跳转
$('.xzh-nav-right').on('click', function() {
    location.href = 'teacherPage.html';
});

//取消新增
$('.xzh-fix-left').on('click', function () {
    location.href = 'teacherPage.html';
});


//确定新增
$('#xzhFixRight').on('click', function () {
    let teacherName = $('.teacher-name').val();
    let teacherTel = $('.teacher-tel').val();
    let sectionId = $('#teacher-bumen').val();
    let teacherStanding = $('#teacher-gongling').val();
    let teacherEmail = $('#teacher-ea').val();
    let sexId = $('#teacher-sex').val();
    let educationId = $('#teacher-jiaoyu').val();
    let teacherEntry = $('#demoTest').val();

    let obj = {
        teacherName,
        teacherTel,
        sectionId,
        teacherStanding,
        teacherEmail,
        sexId,
        educationId,
        teacherEntry
    }

    console.log(obj);

    //判断是否有输入内容
    if(!teacherName || !teacherTel || !sectionId || !teacherStanding || !teacherEmail || !sexId || !educationId || !teacherEntry) {
        return alert('输入内容不能为空');
    }

    //姓名格式判断
    let name = /^[\u4E00-\u9FA5A-Za-z]+$/;
    if(!name.test(teacherName)) {
       return alert('姓名格式错误');
    }

    //邮箱格式判断
    var email = /^[A-Za-z0-9]+([_\.][A-Za-z0-9]+)*@([A-Za-z0-9\-]+\.)+[A-Za-z]{2,6}$/;
    if(!email.test(teacherEmail)) {
        return alert('邮箱格式错误');
    }

    
    
    //电话号码格式判断
    var tel = /^((\+|00)86)?1((3[\d])|(4[5,6,7,9])|(5[0-3,5-9])|(6[5-7])|(7[0-8])|(8[\d])|(9[1,8,9]))\d{8}$/;
    if(!tel.test(teacherTel)) {
        return alert('电话号码格式错误');
    }
    
    // 1、中国手机号(严谨), 根据工信部2019年最新公布的手机号段
    // /^((\+|00)86)?1((3[\d])|(4[5,6,7,9])|(5[0-3,5-9])|(6[5-7])|(7[0-8])|(8[\d])|(9[1,8,9]))\d{8}$/
    
    // 2、中国手机号(宽松), 只要是13,14,15,16,17,18,19开头即可
    // /^((\+|00)86)?1[3-9]\d{9}$/
    
    // 3、中国手机号(最宽松), 只要是1开头即可, 如果你的手机号是用来接收短信, 优先建议选择这一条
    // /^((\+|00)86)?1\d{10}$/

    $.ajax ({
        url: '/teachers/add',
        method: 'POST',
        data: obj
    })
    .done (function (res){
        console.log('新增成功:',res);
        // alert('新增成功');
        location.href = 'teacherPage.html';
        // initTeacher();
    })
    .fail (function (err) {
        console.log('新增失败:',err);
        // alert('新增失败');
    })
})